"""
货物相关的工具函数
"""
from typing import Dict, Any, Optional, List
from app.models.shipment import ShipmentMonitor
from app.schemas.shipment import ShipmentUpdate


def detect_field_changes(original: ShipmentMonitor, update_data: ShipmentUpdate) -> Dict[str, Any]:
    """
    检测货物信息中的字段变化
    
    Args:
        original: 原始货物数据
        update_data: 更新数据
        
    Returns:
        Dict包含变化信息:
        - mac_id_changed: 设备号是否变化
        - pickup_warehouse_changed: 取货仓库是否变化
        - delivery_warehouse_changed: 卸货仓库是否变化
        - changed_fields: 变化的字段列表
        - old_values: 原始值
        - new_values: 新值
    """
    changes = {
        'mac_id_changed': False,
        'pickup_warehouse_changed': False,
        'delivery_warehouse_changed': False,
        'changed_fields': [],
        'old_values': {},
        'new_values': {}
    }
    
    # 检查设备号变化
    if hasattr(update_data, 'mac_id') and update_data.mac_id is not None:
        if update_data.mac_id != original.mac_id:
            changes['mac_id_changed'] = True
            changes['changed_fields'].append('mac_id')
            changes['old_values']['mac_id'] = original.mac_id
            changes['new_values']['mac_id'] = update_data.mac_id
    
    # 检查取货仓库变化
    if hasattr(update_data, 'pickup_warehouse') and update_data.pickup_warehouse is not None:
        if update_data.pickup_warehouse != original.pickup_warehouse:
            changes['pickup_warehouse_changed'] = True
            changes['changed_fields'].append('pickup_warehouse')
            changes['old_values']['pickup_warehouse'] = original.pickup_warehouse
            changes['new_values']['pickup_warehouse'] = update_data.pickup_warehouse
    
    # 检查卸货仓库变化
    if hasattr(update_data, 'delivery_warehouse') and update_data.delivery_warehouse is not None:
        if update_data.delivery_warehouse != original.delivery_warehouse:
            changes['delivery_warehouse_changed'] = True
            changes['changed_fields'].append('delivery_warehouse')
            changes['old_values']['delivery_warehouse'] = original.delivery_warehouse
            changes['new_values']['delivery_warehouse'] = update_data.delivery_warehouse
    
    return changes


def should_validate_device(changes: Dict[str, Any]) -> bool:
    """
    判断是否需要验证设备
    
    Args:
        changes: 字段变化信息
        
    Returns:
        是否需要验证设备
    """
    return changes['mac_id_changed']


def should_update_coordinates(changes: Dict[str, Any]) -> bool:
    """
    判断是否需要更新坐标
    
    Args:
        changes: 字段变化信息
        
    Returns:
        是否需要更新坐标
    """
    return changes['pickup_warehouse_changed'] or changes['delivery_warehouse_changed']


def get_coordinate_update_fields(changes: Dict[str, Any]) -> Dict[str, bool]:
    """
    获取需要更新坐标的字段
    
    Args:
        changes: 字段变化信息
        
    Returns:
        Dict包含需要更新的字段:
        - update_pickup: 是否更新取货坐标
        - update_delivery: 是否更新卸货坐标
    """
    return {
        'update_pickup': changes['pickup_warehouse_changed'],
        'update_delivery': changes['delivery_warehouse_changed']
    }
